package day0113;

import java.util.Scanner;

import org.junit.Test;

public class HomeWork {
	//1.实现trim
	@Test
	public void test1() {
		int start = 0;
		int end = 0;
		char kongge = ' ';
		System.out.println("请输入字符串");
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		int length = str.length();
		char[] array = str.toCharArray();
		if (str == null || str.equals("") ) {
            System.out.println("代码有误");
       }
		for (int i = 0; i < array.length; i++) {
			if (array[i] == kongge) {
				start++;
			}else {
				break;
			}
		}
		for (int i = array.length - 1;  i > 0; i--) {
			if (array[i] == kongge ) {
				end++;
			} else {
				break;
			}
			} if (start > end) {
            System.out.println("全是空格");
			} else {
    	   System.out.println(str.substring(start, length - end));
			}
	}
	
	@Test
	public void test2() {
		String str = "I LOVE Java!";
		System.out.println(str.toLowerCase());
	}
	
	@Test
	public void test3() {
		String str = "I Love Java!";
		char[] array = str.toCharArray();
		array[0] = 'I' + 32;
		array[2] = 'L' + 32;
		array[7] = 'J' + 32;
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i]);
		}
	}
	
	@Test
	public void test4() {
		int count = 0;
		String str = "this is my homework! I must finish it!";
		String[] array = str.split(" ");
		for (int i = 0; i < array.length; i++) {
			count++;
			System.out.println(array[i]);
		}
		System.out.println(count + "个单词");
		System.out.println(array.length + "个单词");
	}
	
	@Test
	public void test5() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入字符串：");
		String str = scanner.nextLine();
		char[] array = toLowerCase(str);
		// System.out.println(Arrays.toString(array));
		System.out.println(array);
	}
	//封装的方法
	private char[] toLowerCase(String str) {
		char[] array = str.toCharArray();
		for (int i = 0; i < array.length; i++) {
			if (array[i] >= 'A' && array[i] <= 'Z') {
				array[i] = (char)(array[i] + 32);
			}
		}
		return array;
	}
}


